import 'dart:io';
import 'dart:typed_data';

// import 'package:fluwx/fluwx.dart';
// import 'package:tencent_kit/tencent_kit.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:friends_flutter/utils/xd_file_utils.dart';

import 'date_utils.dart';
import 'loding.dart';

class ShareUtils {

  ///分享链接追加mark
  static String appendShareUrl(String targetUrl,
      {String? mark}) {
    if (mark != null) {
      String s = '?';
      if (targetUrl.contains('?')) s = '&';
      StringBuffer sb = StringBuffer(targetUrl);
      sb
        ..write(s)
        ..write('mark=')
        ..write(mark ?? '');
      return sb.toString();
    }

    // if (DeviceInfo().userInfo != null) {
    //   String s = '?';
    //   if (targetUrl.contains('?')) s = '&';
    //   StringBuffer sb = StringBuffer(targetUrl);
    //   sb
    //     ..write(s)
    //     ..write('inviteCode=')
    //     ..write(DeviceInfo().userInfo?.user?.inviteCode ?? '');
    //   return sb.toString();
    // }
    return targetUrl;
  }

  ///
  ///分享平台及内容
  ///platform 1、微信分享 2、朋友圈分享 3、QQ分享 4、QQ空间分享 5、复制链接 6、系统分享
  ///showType 1: 邀请函分享
  static void onShare(int platform, String title, String body, String targetUrl, {String? img, showType = 0, String? mark}) {
    targetUrl = appendShareUrl(targetUrl, mark: mark);

    print('targetUrl = ${targetUrl}');

    String sharePlatform = '';
    switch (platform) {
      case 1: //微信
        sharePlatform = 'wechat';
        // shareToWeChat(WeChatShareWebPageModel(targetUrl,
        //     title: title, description: body, thumbnail: WeChatImage.asset('assets/images/common/xdan_logo_sq.png'), scene: WeChatScene.SESSION));
        break;

      case 2: //朋友圈
        sharePlatform = 'wechat_friend_circle';
        // shareToWeChat(WeChatShareWebPageModel(targetUrl,title: title,
        //     description: body, thumbnail: WeChatImage.asset('assets/images/common/xdan_logo_sq.png'), scene: WeChatScene.TIMELINE));
        break;

      case 3: //QQ
        // sharePlatform = 'qq';
        // Tencent.instance.shareWebpage(
        //     scene: TencentScene.SCENE_QQ,
        //     title: title,
        //     summary: body,
        //     targetUrl: targetUrl);
        break;

      case 4: //QQ空间
        // sharePlatform = 'qq_friend_circle';
        // Tencent.instance.shareWebpage(
        //     scene: TencentScene.SCENE_QZONE,
        //     title: title,
        //     summary: body,
        //     targetUrl: targetUrl);
        break;

      case 5: //复制链接
        FileUtils.copyClipboard(targetUrl);
        break;

      case 6: //系统分享
        // Share.share(body, subject: targetUrl);
        break;

    }
  }

  ///
  ///分享图片
  ///platform 1、微信分享 2、朋友圈分享 3、QQ分享 4、QQ空间分享 5、复制链接 6、系统分享
  ///showType 1: 邀请函分享
  static void onShareImg(int platform, Uint8List? imgBytes,
      {String? img, int showType = 0,}) async {
    String sharePlatform = '';
    switch (platform) {
      case 1: //微信
        sharePlatform = 'wechat';
        // shareToWeChat(WeChatShareImageModel(WeChatImage.binary(imgBytes!),
        //     scene: WeChatScene.SESSION));
        break;

      case 2: //朋友圈
        sharePlatform = 'wechat_friend_circle';
        // shareToWeChat(WeChatShareImageModel(WeChatImage.binary(imgBytes!),
        //     scene: WeChatScene.TIMELINE));
        break;

      case 3: //QQ
        sharePlatform = 'qq';
        final dir = await path_provider.getTemporaryDirectory();
        final targetPath =
            "${dir.absolute.path}/${XdDateUtils.getCurrTimeStamp()}.jpg";
        File(targetPath).writeAsBytes(imgBytes!).then((value) {
          // Tencent.instance.shareImage(
          //     scene: TencentScene.SCENE_QQ, imageUri: Uri.file(value.path));
        });
        break;

      case 4: //QQ空间
        sharePlatform = 'qq_friend_circle';
        xdShowToast('暂不支持分享到QQ空间～');
        //QQ空间不支持分享图片
       // final dir = await path_provider.getTemporaryDirectory();
       // final targetPath = dir.absolute.path + "/${LYDateUtils.getCurrTimeStamp()}.jpg";
//        File(targetPath).writeAsBytes(imgBytes!).then((value){
//          LogUtil.println('aaaaaaaaaaa');
//          LogUtil.println(value.path);
//          Tencent.instance.shareImage(
//              scene: TencentScene.SCENE_QZONE,
//              imageUri: Uri.file(value.path));
//        });
        break;
      case 6: //系统分享
        // Share.shareXFiles([XFile.fromData(imgBytes!, mimeType: 'image/png', name: 'xdan_${XdDateUtils.getCurrTimeStamp()}.png',)],);
        break;
    }
  }

}